package com.furkid.service.service.impl;

import com.furkid.service.common.exception.BizException;
import com.furkid.service.constant.AjaxResult;
import com.furkid.service.constant.HttpStatus;
import com.furkid.service.domain.pet.Pet;
import com.furkid.service.domain.pet.PetBreed;
import com.furkid.service.domain.pet.PetType;
import com.furkid.service.domain.pet.PetWeight;
import com.furkid.service.mapper.PetMapper;
import com.furkid.service.service.IPetService;
import com.furkid.service.web.context.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class IPetServiceImpl implements IPetService {
    @Autowired
    PetMapper petMapper;

    @Override
    public PageInfo<Pet> listPets(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Pet> list = petMapper.listPets(UserContext.getUserId());
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Pet> serachPetByName(int pageNum, int pageSize, String name) {
        PageHelper.startPage(pageNum, pageSize);
        List<Pet> list = petMapper.searchPetByName(UserContext.getUserId(), name);
        return new PageInfo<>(list);
    }

    @Override
    public Pet searchPetById(Long petId) {
        Pet pet = petMapper.searchPetById(UserContext.getUserId(), petId);
        if (pet == null) {
            throw new BizException(HttpStatus.Error, "宠物不存在");
        }
        return pet;
    }

    @Override
    public List<PetType> petTypeList() {
        return petMapper.petTypeList();
    }

    @Override
    public List<PetBreed> petBreedList(Integer petTypeId) {
        return petMapper.petBreedList(petTypeId);
    }

    @Override
    public boolean addPet(Pet pet) {
        return petMapper.addPet(pet) == 1;
    }

    @Override
    public void deletePetById(Long petId) {
        Pet pet = petMapper.selectPetById(petId);
        if (pet == null) {
            throw new BizException(HttpStatus.Error, "宠物不存在");
        }

        int rows = petMapper.deletePetById(petId);
        if (rows == 0) {
            throw new BizException(HttpStatus.INTERNAL_SERVER_ERROR, "删除失败，请稍后重试");
        }
    }

    @Override
    public List<PetWeight> petWeightListById(Long petId) {
        Pet pet = petMapper.searchPetById(UserContext.getUserId(), petId);
        if (pet == null) {
            throw new BizException(HttpStatus.Error, "用户没有该宠物，无法查询");
        }

        List<PetWeight> petWeightList = petMapper.petWeightListById(petId);
        if (petWeightList == null) {
            throw new BizException(HttpStatus.Error, "列表不存在");
        }
        return petWeightList;
    }

    @Override
    public void addOnePetWeight(PetWeight petWeight) {
        Pet pet = petMapper.searchPetById(UserContext.getUserId(), petWeight.getPetId());
        if (pet == null) {
            throw new BizException(HttpStatus.Error, "用户没有该宠物，无法添加");
        }

        int res = petMapper.addOnePetWeight(petWeight);
        if (res == 0) {
            throw new BizException(HttpStatus.Error, "添加失败，未知错误!!!");
        }
    }

    @Override
    public void updateOnePetWeight(PetWeight petWeight) {
        if (petWeight.getId() == null) {
            throw new BizException(HttpStatus.Error, "缺少体重记录ID，无法修改");
        }

        Pet pet = petMapper.searchPetById(UserContext.getUserId(), petWeight.getPetId());
        if (pet == null) {
            throw new BizException(HttpStatus.Error, "用户没有该宠物，无法修改");
        }

        int res = petMapper.updateOnePetWeight(petWeight);
        if (res == 0) {
            throw new BizException(HttpStatus.Error, "更新失败，未知错误!!!");
        }
    }

    @Override
    public void deleteOnePetWeight(Long id, Long petId) {
        if (petId == null || petId == 0) {
            throw new BizException(HttpStatus.Error, "缺少体重记录ID，无法修改");
        }

        Pet pet = petMapper.searchPetById(UserContext.getUserId(), petId);
        if (pet == null) {
            throw new BizException(HttpStatus.Error, "用户没有该宠物，无法删除");
        }

        int res = petMapper.deleteOnePetWeight(id, petId);
        if (res == 0) {
            throw new BizException(HttpStatus.Error, "更新失败，未知错误!!!");
        }
    }
}
